//
//  MainViewController.swift
//  Swift weibo
//
//  Created by 美食家 on 16/10/9.
//  Copyright © 2016年 不孤独的美食家. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        tabBar.tintColor=UIColor.orange
        addChildControllers()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.addSubview(composeButton)
        let rect = composeButton.frame
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count)
        LHLog(message: childViewControllers.count)
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: rect.height)
    }
    // MARK: - 内部控制方法
   private func addChildControllers() -> Void {
        
     guard let path = Bundle.main.path(forResource: "MainVCSettings.json", ofType: nil) else{
        return
        }
        
     guard let data = NSData(contentsOfFile: path) else
        {
            return
        }
        do
        {
            let objc = try JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! [[String: AnyObject]]
            
            for dict in objc
            {
                let title = dict["title"] as? String
                let vcName = dict["vcName"] as? String
                let imageName = dict["imageName"] as? String
                addChildController(childController: vcName, title: title, imgName: imageName)
            }
        }catch
        {
            addChildController(childController:"HomeTableViewController", title: "首页", imgName: "tabbar_home")
            addChildController(childController:"MessageTableViewController", title: "消息", imgName: "tabbar_message_center")
            addChildController(childController:"NullViewController", title: "", imgName: "")
            addChildController(childController:"DiscoverTableViewController", title: "发现", imgName: "tabbar_discover")
             addChildController(childController:"ProfileTableViewController", title: "我", imgName: "tabbar_profile")
        }
    }
    func addChildController(childController: String?, title: String?, imgName: String?) -> Void {
        
        guard let name =  Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else
        {
            return
        }
        var cls: AnyClass? = nil
        if let vcName = childController
        {
            cls = NSClassFromString(name + "." + vcName)
        }

        guard let typeCls = cls as? UITableViewController.Type else
        {
            LHLog(message: "cls不能当做UITableViewController")
            return
        }

        let childController = typeCls.init()

        childController.title = title
        if let ivName = imgName
        {
            childController.tabBarItem.image = UIImage(named: ivName)
            childController.tabBarItem.selectedImage = UIImage(named: ivName + "_highlighted")
        }
        let nav = UINavigationController(rootViewController:childController)
        addChildViewController(nav)
    }
    /// 监听加号按钮点击
   @objc private func compseBtnClick(btn: UIButton)
    {
        LHLog(message: btn)
    }
    // MARK: - 懒加载
    private lazy var composeButton: UIButton = {
        () -> UIButton
        in
        // 1.创建按钮
        let btn = UIButton(imageName:"tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
        // 2.监听按钮点击
        btn.addTarget(self, action: Selector(("compseBtnClick:")), for: UIControlEvents.touchUpInside)
        return btn
    }()

  }

